package lq.demo.springbootmybatis.demo.controller;

import lq.demo.springbootmybatis.demo.common.annotation.Log;
import lq.demo.springbootmybatis.demo.entity.MyRespon;
import lq.demo.springbootmybatis.demo.entity.User;
import lq.demo.springbootmybatis.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    MyRespon respon;

    @Log("获取用户列表")
    @RequestMapping("/getUser")
    public MyRespon getUser() {
//        return userService.getUser();
       /* return new HashMap<String, Object>(){{
            put("code",200);
            put("name","张三");
        }};*/
        System.out.println("查询了user");
        return respon.ok().setResult(userService.getUser());
    }

    /**
     *
     * @param userList
     * @return
     */
    @Log("批量添加用户")
    @PostMapping("addBatchUser")
    public MyRespon addBatchUser(@RequestBody List<User> userList) {
        return respon.ok().setResult(userService.addBatchUser(userList));
    }
    @Log("修改用户")
    @PostMapping("updateUser")
    public MyRespon updateUser(@RequestBody User user){
        return respon.ok().setResult(userService.updateUser(user));
    }
    @Log("删除用户")
    @PostMapping("deleteUser")
    public MyRespon deleteUser(@RequestBody User user){
        return respon.ok().setResult(userService.deleteUser(user));
    }

    /**
     * 删除数据,然后形成异常,看是否回滚
     * @param user
     * @return
     */
    @Log("错误请求回退")
    @PostMapping("error")
    public MyRespon error(@RequestBody User user){
        return respon.ok().setResult(userService.error(user));
    }
}
